home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / doSubdivCreate.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  8.0 KB  |  277 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //  Alias|Wavefront Script File
  18. //  MODIFY THIS AT YOUR OWN RISK
  19. //
  20. //  Creation Date:  12 March, 1999
  21. //  Author:         nrohm
  22. //
  23. //  Description:
  24. //      Functions called from "Subdiv Create".
  25. //
  26.  
  27.  
  28. //
  29. //  Procedure Name:
  30. //      doSubdivCreate
  31. //
  32. //  Description:
  33. //        This is the actual function that gets called from "Subdiv Create" 
  34. //      option box.
  35. //
  36. //  Input Arguments:
  37. //        $version: The version of this option box.  This is used to know how to 
  38. //                  interpret the $args array.
  39. //      $args:
  40. //      If Version 1:
  41. //      [0]  $origObjectAction:  what to do with the original object
  42. //                               options are: replace, hide, or show it
  43. //      If Version 2:
  44. //      [1]  $globalHist:        global construction history setting
  45. //
  46. //      [2]  $maxPolys:          max polys that can be converted (if creating
  47. //                               from poly
  48. //
  49. //      [3]  $maxEdgesPerVert:   max edges per vertex of a polymesh that
  50. //                               can be handled
  51. //                               
  52. //
  53. //  Return Value:
  54. //      None.
  55. //
  56. //  Note:
  57. //
  58. global proc int doSubdivCreate( string $version, string $args[] )
  59. {
  60.     int $status = 0;
  61.     int $versionNum = $version;
  62.  
  63.     if ($versionNum == 1 && size($args) != 1){
  64.         error("Incorrect number of arguments for doSubdivCreate.");
  65.         return $status;
  66.     }
  67.  
  68.     if ($versionNum == 2 && size($args) < 4){
  69.         error("Incorrect number of arguments for doSubdivCreate.");
  70.         return $status;
  71.     }
  72.  
  73.     if ($versionNum == 3 && size($args) < 5){
  74.         error("Incorrect number of arguments for doSubdivCreate.");
  75.         return $status;
  76.     }
  77.  
  78.     int $origObjectAction = $args[0];
  79.     int $globalHist       = ($versionNum >= 2 ? $args[1] : 0);
  80.     int $maxPolys         = ($versionNum >= 2 ? $args[2] : 1000); 
  81.     int $maxEdgesPerVert  = ($versionNum >= 2 ? $args[3] : 32);
  82.     int $ap  = ($versionNum >= 3 ? $args[4] : 0);
  83.  
  84.     // extract all poly meshes
  85.     string $polyList[] = `filterExpand -ex true -fp true -sm 12`; 
  86.     int    $polyLen = size($polyList);
  87.  
  88.     // extract all nurbs
  89.     string $nurbsList[] = `filterExpand -ex true -fp true -sm 10`; 
  90.     int    $nurbsLen = size($nurbsList);
  91.  
  92.     int    $len = $polyLen + $nurbsLen;
  93.  
  94.     if ($len == 0) {
  95.       error ("Select a polygon or a NURBS surface " +
  96.              "from which you want to create a subdivision surface.");
  97.         return $status;
  98.     }
  99.  
  100.     string $polyCmd = "polyToSubdiv -ap " + $ap + " ";
  101.     string $nurbsCmd = "nurbsToSubdiv -rn false -mp true ";
  102.  
  103.     // set up commands with appropriate options
  104.     //
  105.     switch ($origObjectAction) {
  106.       case 0:
  107.       case 1:
  108.         // replace original object, forces history off, but delete
  109.         // will take care of that...
  110.         $polyCmd = $polyCmd + "-ch off -aut on ";
  111.         $nurbsCmd = $nurbsCmd + "-ch off -aut on ";
  112.         break;
  113.  
  114.       case 2:
  115.         // forces history on
  116.         $polyCmd = $polyCmd + "-ch on -aut on ";
  117.         $nurbsCmd = $nurbsCmd + "-ch on -aut on ";
  118.         if( !$globalHist ) {
  119.             warning( "Forcing construction history to on." );
  120.         }
  121.         break;
  122.  
  123.       case 3:
  124.         // show original object, respects global construction history
  125.         $polyCmd = $polyCmd + "-ch 0 -aut off ";
  126.         $nurbsCmd = $nurbsCmd + "-ch 0 -aut off ";
  127.         break;
  128.     }
  129.  
  130.     $polyCmd  += (" -maxPolyCount " + $maxPolys + " ");
  131.     $polyCmd  += (" -maxEdgesPerVert " + $maxEdgesPerVert + " ");
  132.  
  133.     // nurbs uses max polys as well
  134.     //
  135.     $nurbsCmd  += (" -maxPolyCount " + $maxPolys + " ");
  136.  
  137.     // Create
  138.     string $selList = "";
  139.       string $polyRes[];
  140.       string $nurbsRes[];
  141.       int    $retVal;
  142.  
  143.     //  Convert each polymesh, one at a time so we can catch errors
  144.     //
  145.     for($i = 0; $i < $polyLen; $i++) {
  146.         int $smart = 1;
  147.         if ($smart) {
  148.             string $pruneCmd = "deleteInternalValence2Verts "+$polyList[$i]+";\n";
  149.             $pruneCmd = $pruneCmd + $polyCmd + $polyList[$i];
  150.             $retVal = catch ($polyRes = evalEcho($pruneCmd));
  151.         } else {
  152.             $retVal = catch ($polyRes = evalEcho ($polyCmd + $polyList[$i]));
  153.         }
  154.         
  155.         // if there's no error, do post work and add results 
  156.         if ($retVal != 1){
  157.             switch ($origObjectAction){
  158.               case 0:
  159.               case 1:
  160.                 string $polyParents[] = `listRelatives -allParents $polyList[$i]`;
  161.                 if( size($polyParents) <= 1 ) {
  162.                     // delete original only if original poly has only one
  163.                     // parent. 
  164.                     //
  165.                     evalEcho ("delete "  + $polyList[$i]);
  166.                 }
  167.                 else {
  168.                     // If original poly has more than one original, remove
  169.                     // the shape as a child of the selected transform using
  170.                     // the parent command - fix for bug #150166.
  171.                     //
  172.                     evalEcho ("parent -removeObject -shape " + $polyList[$i]);
  173.                 }
  174.                 // Since "$polyRes" is being filled in the "for" loop, the
  175.                 // first entry is the entry of interest for every iteration.
  176.                 //
  177.                 string $parent[] = `listRelatives -parent $polyRes[0]`;
  178.                 rename $parent[0] polyToSubd1;
  179.                 break;
  180.  
  181.               case 2:
  182.                 // disconnect the shader:
  183.                 string $shade[] = `listConnections -p 1 ($polyList[$i] + ".iog")`;
  184.                 int $nShade = size($shade);
  185.                 int $j;
  186.                 for( $j=0; $j<$nShade; $j+=1 ) {
  187.                     disconnectAttr ($polyList[$i] + ".iog") $shade[$j];
  188.                 }
  189.                 //
  190.                 // When creating the subd and keeping the original,
  191.                 // we want the new subd shape to be the first child, so
  192.                 // its menu will appear on RMB.
  193.                 reorder -f $polyRes[$i];
  194.                 break;
  195.  
  196.               case 3:
  197.               default:
  198.                 break;
  199.             }
  200.  
  201.             for ($s in $polyRes) {
  202.                 $selList += ($s + " ");
  203.             }
  204.         }
  205.     }
  206.  
  207.     //  Convert each nurbs, one at a time so we can catch errors
  208.     //
  209.     for($i = 0; $i < $nurbsLen; $i++) {
  210.         $retVal = catch ($nurbsRes = evalEcho ($nurbsCmd + $nurbsList[$i]));
  211.  
  212.         // if there's no error, do post work and add results 
  213.         if ($retVal != 1){
  214.             switch ($origObjectAction){
  215.               case 0:
  216.               case 1:
  217.                 string $nurbsParents[] = `listRelatives -allParents $nurbsList[$i]`;
  218.                 if( size($nurbsParents) <= 1 ) {
  219.                     // delete original only if original nurbs has only one
  220.                     // parent. 
  221.                     //
  222.                     evalEcho ("delete "  + $nurbsList[$i]);
  223.                 }
  224.                 else {
  225.                     // If original nurbs has more than one original, remove
  226.                     // the shape as a child of the selected transform using
  227.                     // the parent command - fix for bug #150166.
  228.                     //
  229.                     evalEcho ("parent -removeObject -shape " + $nurbsList[$i]);
  230.                 }
  231.                 // Since "$nurbsRes" is being filled in the "for" loop, the
  232.                 // first entry is the entry of interest for every iteration.
  233.                 //
  234.                 string $parent[] = `listRelatives -parent $nurbsRes[0]`;
  235.                 rename $parent[0] nurbsToSubd1;
  236.                 break;
  237.               case 2:
  238.                 break;
  239.               case 3:
  240.               default:
  241.                 break;
  242.             }
  243.  
  244.             for ($s in $nurbsRes) {
  245.                 $selList += ($s + " ");
  246.             }
  247.         }
  248.     }
  249.  
  250.     // In case our new subdiv shapes have no shader,
  251.     // assign them to the default initialShadingGroup.
  252.     // This can happen if users delete the subdiv shape
  253.     // while in polygon-proxy mode, and then create a new
  254.     // subdiv from the polygon-proxy.
  255.     //
  256.     // It's easier to work with an array of results...
  257.     //
  258.     string  $results[];
  259.     string  $r;
  260.     if( size( $selList ) > 0 ) {
  261.         tokenize( $selList, " ", $results );
  262.     }
  263.  
  264.     for( $r in $results ) {
  265.         subdAssignDefaultShader( $r );
  266.     }
  267.  
  268.     if (size($selList) > 0){
  269.           $selList += ";";
  270.         eval ("select -r " + $selList);
  271.     }
  272.  
  273.     return $status;
  274. }
  275.  
  276.  
  277.